#!/usr/bin/env julia
"""
Debug test script to systematically test each module
"""

using Pkg
Pkg.activate(".")

println("🔧 GSI Core Analysis - Debug Test")
println("=" ^ 50)

# Test 1: Basic loading
println("\n📋 Test 1: Module Loading")
try
    using GSICoreAnalysis
    println("  ✅ Main package loaded")
    
    using GSICoreAnalysis.GridOperations
    println("  ✅ GridOperations loaded")
    
    using GSICoreAnalysis.ControlVectors
    println("  ✅ ControlVectors loaded")
    
    using GSICoreAnalysis.StateVectors
    println("  ✅ StateVectors loaded")
    
    using GSICoreAnalysis.BackgroundError
    println("  ✅ BackgroundError loaded")
    
    # This might be the problematic one
    println("  🔍 Testing CostFunctions...")
    using GSICoreAnalysis.CostFunctions
    println("  ✅ CostFunctions loaded")
    
    using GSICoreAnalysis.Minimization
    println("  ✅ Minimization loaded")
    
catch e
    println("  ❌ Module loading failed: $e")
end

# Test 2: Basic vector creation
println("\n📊 Test 2: Vector Creation")
try
    config = AnalysisConfig(grid_size=(10,10,5))
    println("  ✅ Config created: $(config.grid_size)")
    
    sv = StateVector(config)
    println("  ✅ StateVector created: $(size(sv.u))")
    
    cv = ControlVector(config)
    println("  ✅ ControlVector created: $(length(cv.values))")
    
catch e
    println("  ❌ Vector creation failed: $e")
end

# Test 3: Background Error
println("\n🔄 Test 3: Background Error")
try
    config = AnalysisConfig(grid_size=(10,10,5))
    bg_error = BackgroundError.HybridBMatrix(config)
    println("  ✅ HybridBMatrix created")
    
catch e
    println("  ❌ Background error failed: $e")
end

# Test 4: Cost Function - this is likely where it hangs
println("\n💰 Test 4: Cost Function")
try
    config = AnalysisConfig(grid_size=(10,10,5))
    println("  🔍 Creating HybridCostFunction...")
    cost_fn = CostFunctions.HybridCostFunction(config)
    println("  ✅ HybridCostFunction created")
    
catch e
    println("  ❌ Cost function failed: $e")
end

println("\n✨ Debug test completed!")